/**
 * @date 2016年1月7日 下午12:32:22
 * @version V1.0
 */
package edu.cn.nlsde.tmfst.EVA;

import edu.cn.nlsde.Utils.IOUtil;
import edu.cn.nlsde.Utils.Utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


/**
 * @author ffftzh
 * @ClassName: Web_coherence
 * @Description: TODO
 * @date 2016年1月7日 下午12:32:22
 */
public class Web_coherence implements Runnable {

    static ArrayList<ArrayList<ArrayList<String>>> topic_words = new ArrayList<ArrayList<ArrayList<String>>>();

    int corpusNum = 0, topN = 0;
    String storePath = "";

    public Web_coherence(int corpusNum, int topN, String storePath) {
        this.corpusNum = corpusNum;
        this.topN = topN;
        this.storePath = storePath;
    }

    public static double log(double value, double base) {
        return Math.log(value) / Math.log(base);
    }

    public static HashMap<Integer, String> readWordMap(String wordMap_path) throws IOException {
        HashMap<Integer, String> result = new HashMap<Integer, String>();
        BufferedReader reader = IOUtil.getReader(wordMap_path);
        String line = null;
        while ((line = reader.readLine()) != null) {
            String[] lines = line.split("\\s+");
            if (lines.length != 2) {
                continue;
            }
            result.put(Integer.parseInt(lines[1]), lines[0]);
        }
        reader.close();
        return result;
    }

    public static void readTopic_words(String phi_path, String wordMap_path) throws IOException {
        HashMap<Integer, String> wordMap = readWordMap(wordMap_path);
        BufferedReader reader = IOUtil.getReader(phi_path);
        String line = null;
        ArrayList<ArrayList<String>> theOneSet = new ArrayList<ArrayList<String>>();

        while ((line = reader.readLine()) != null) {
            String[] lines = line.split("\\s+");
            HashMap<Integer, Double> lineDuoble = new HashMap<Integer, Double>();
            for (int i = 0; i < lines.length; i++) {
                lineDuoble.put(i, Double.parseDouble(lines[i]));
            }
            ArrayList<Entry<Integer, Double>> tmpentryList = new ArrayList<Entry<Integer, Double>>(lineDuoble.entrySet());
            Collections.sort(tmpentryList, new Comparator<Entry<Integer, Double>>() {
                public int compare(Entry<Integer, Double> o1, Entry<Integer, Double> o2) {
                    return (o2.getValue().compareTo(o1.getValue()));
                }
            });
            ArrayList<String> oneLineResult = new ArrayList<String>();
            for (int i = 0; i < 20; i++) {
                String thestemword = wordMap.get(tmpentryList.get(i).getKey());
                oneLineResult.add(thestemword);
            }
            theOneSet.add(oneLineResult);
        }
        topic_words.add(theOneSet);
        reader.close();
    }

    public static void main(String[] args) throws IOException {
        String[] dirStrings = {"LDA-0.01-0.01", "PAM4ST(0.01_innerstep_1)", "SP_SATM", "unigram", "DsparseTM", "wntm"};
        String[] phiStrings = {"model-final.phi", "phi.txt", "phi.txt", "mu_phi_1.txt", "model-final.phi", "model-final.phi"};
        String[] wordMapStrings = {"wordmap.txt", "wordmap.txt", "wordmap.txt", "mu_wordmap.txt", "wordmap.txt", "wordmap.txt"};

        ExecutorService service = Executors.newCachedThreadPool();
        for (int me = 0; me < 6; me++) {
            String root = "data2/news/" + dirStrings[me] + "/";
            for (int i = 0; i < 5; i++) {
                String rootPath = root + i + "/";
                Web_coherence.readTopic_words(rootPath + phiStrings[me], rootPath + wordMapStrings[me]);
                service.submit(new Web_coherence(me * 5 + i, 10, rootPath + "10.cohrence"));
                service.submit(new Web_coherence(me * 5 + i, 15, rootPath + "15.cohrence"));
            }
        }
        service.shutdown();

    }

    @Override
    public void run() {
        double t_score = 0.0;
        BufferedWriter writer = IOUtil.getWriter(storePath);
        try {
            for (int k = 0; k != topic_words.get(corpusNum).size(); k++) {
                double score = 0.0;
                String requestUrl = "http://palmetto.aksw.org/palmetto-webapp/service/cv";
                String requestParam = "words=" + Utils.join(topic_words.get(corpusNum).get(k).subList(0, topN), "%20");
                score = Double.parseDouble(Utils.sendGet(requestUrl, requestParam));
                writer.write("\ttopic " + k + "\t" + score + "\n");
                t_score += score;
                System.out.print("topic " + k + "\n");
            }
            writer.write("avg:\t" + t_score / topic_words.get(corpusNum).size() + "\n");
        } catch (Exception es) {
            es.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

}
